import { ElMessage, MessageParams, MessageHandle } from 'element-plus'

type IType = 'success' | 'warning' | 'info' | 'error'
type IMessageParams = MessageParams
type IMessageHandle = MessageHandle

const showMessage = Symbol('showMessage') // 私有showMessage
let messageInstance: IMessageHandle

class resetMessage {
	success(options: IMessageParams, single = true) {
		this[showMessage]('success', options, single)
	}
	warning(options: IMessageParams, single = true) {
		this[showMessage]('warning', options, single)
	}
	info(options: IMessageParams, single = true) {
		this[showMessage]('info', options, single)
	}
	error(options: IMessageParams, single = true) {
		this[showMessage]('error', options, single)
	}

	[showMessage](type: IType, options: IMessageParams, single: boolean) {
		if (messageInstance && single) {
			messageInstance.close() // 关闭原来的
		}
		messageInstance = ElMessage[type](options) // 新建新的消息实例
	}
}

export default new resetMessage()
